package com.asialjim.wechat.apiservice.impl;

import com.asialjim.wechat.apiservice.SecondAccessTokenCache;
import com.asialjim.wechat.pojo.accesstoken.AccessToken;
import lombok.Data;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: SecondAccessTokenCacheImpl ]</i></h2><strong>
 * <p> second access token cache impl
 * <p> base redisson
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/3   &nbsp;&nbsp; JDK 8
 */
@Data
public class SecondAccessTokenCacheImpl implements SecondAccessTokenCache {
    private RedissonClient redisson;

    @Override
    public AccessToken get(String appId) {
        return (AccessToken)redisson.getMap(SecondAccessTokenCache.CACHE_NAME).get(appId);
    }

    @Override
    public void set(AccessToken accessToken) {
        RMap<String, AccessToken> cacheMap = redisson.getMap(SecondAccessTokenCache.CACHE_NAME);
        cacheMap.put(accessToken.getAppId(),accessToken);
    }

    @Override
    public void clean(String appId) {
        RMap<String, AccessToken> cacheMap = redisson.getMap(SecondAccessTokenCache.CACHE_NAME);
        cacheMap.remove(appId);
    }

    public void init(){
        if (Objects.isNull(redisson)){
            throw new IllegalStateException("redisson is null");
        }
    }

}